package de.kundepro.example.vaadin;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import de.kundepro.example.vaadin.entities.FirmaRechtsformen;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Query;
import org.eclipse.persistence.queries.UpdateObjectQuery;

public class tmpDataAccessObject1 {

    private javax.persistence.EntityManager em;

    public tmpDataAccessObject1() {

        em = javax.persistence.Persistence.createEntityManagerFactory("KundePro").createEntityManager();



    }

    public ArrayList<Firma> getCompany(String value) {

        ArrayList<Firma> comp = new ArrayList();
        List<Firma> company = (List<Firma>) em.createQuery("select u from Firma u where u.fiID = '" + value+"'").getResultList();

        if(!(company.isEmpty())){
            for(Firma found : company) {
                comp.add(found);
            }
        }
        return comp;
    }



    public ArrayList<FirmaRechtsformen> getRechtsform (String val) {

        Query q1 = em.createNamedQuery("findAllRechtsformen");
        q1.setParameter("fiID", (String) val);
        ArrayList<FirmaRechtsformen>  rechtsf = new ArrayList();
        List<FirmaRechtsformen> rf2 = (List<FirmaRechtsformen>) em.createNamedQuery("findAllRechtsformen").getResultList();

          if(!(rf2.isEmpty())){
            for(FirmaRechtsformen found : rf2) {
                rechtsf.add(found);
            }
        }
        return rechtsf;
    }


    public ArrayList<FirmaEinheiten> getAllCompanyDetails(String value) {

        ArrayList<FirmaEinheiten> comp = new ArrayList();
        List<FirmaEinheiten> company = (List<FirmaEinheiten>) em.createQuery("select u from FirmaEinheiten u where u.fiEiFirmaFID = '" + value+"'").getResultList();

        if(!(company.isEmpty())){
            for(FirmaEinheiten found : company) {
                comp.add(found);
            }
        }
        return comp;
    }

        public void setCompanyDetails(FirmaEinheiten fiEi) {

        FirmaEinheiten comp = fiEi;
        comp.setFiPLZ(fiEi.getFiPLZ());
        UpdateObjectQuery query = new UpdateObjectQuery();
        query.setObject(comp);
        query.dontCascadeParts();
//        em.

        String update = "UPDATE FirmaEinheiten SET fiPLZ = '44444' WHERE fiEiID = '11';";

        try {
            em.getTransaction().begin();
//             String update = ("UPDATE FirmaEinheiten SET fiPLZ = '"+ comp.getFiPLZ()+"' WHERE fiEiID = '"+comp.getFiEiID()+"';");
            em.createQuery(update);
            
            em.getTransaction().commit();
        } catch (Throwable e) {
        }
    }


    public ArrayList<Firma> getAllFirmen() {

        ArrayList<Firma> fir = new ArrayList();


        List<Firma> alleFirmen = (List<Firma>) em.createQuery("select u from Firma u").getResultList();

        if (!(alleFirmen.isEmpty())) {
            for (Firma found : alleFirmen) {
                fir.add(found);
            }
        }

        return fir;
    }

        public ArrayList<Person> getAllPersonen() {

        ArrayList<Person> per = new ArrayList();


        List<Person> allePersonen = (List<Person>) em.createQuery("select u from Person u").getResultList();

        if (!(allePersonen.isEmpty())) {
            for (Person found : allePersonen) {
                per.add(found);
            }
        }

        return per;
    }

}
